<?php

namespace app\common\model;

use think\Exception;

class WeixinExpressOrder extends Base
{

    protected $deleteTime = "delete_time";

    public function wechat()
    {
        return $this->hasOne(WeixinUser::class, 'openid', 'openid');
    }

    public function getExpressByOrderId(int $id = 0)
    {
        if ($id == 0) {
            return false;
        }
        $row = $this->where('order_sn', $id)
            ->where('status', 0)
            ->find();
        if (!$row) {
            return false;
        }
        $express = (new WeixinExpressOrderList())->getExpressByOrderId($row['id']);
        if (empty($express)) {
            return false;
        }
        return $express;
    }

    /**
     * @param string $order_id
     * @return WeixinExpressOrder|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getExpressByOrderIdByInfo(string $order_id = "")
    {
        return $this->where('order_id', $order_id)
            ->find();
    }
    public function getOrderByOpenidList(array $where = [], string $openid = '', int $limit = 10)
    {
        try {
            $row = $this
                ->where('openid', $openid)
                ->where($where)
                ->order('id desc')
                ->paginate($limit)
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("订单查询失败");
        }
        return $row;
    }

    /**
     * @param int $order_sn
     * @return WeixinExpressOrder|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByOrderSnByInfo(int $order_sn = 0){
        return $this->where('order_sn',$order_sn)
            ->find();
    }
    public function shop(){
        return $this->hasOne(MallShop::class,'id','shop_id');
    }
}